package com.xcd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcd.reggie.common.R;
import com.xcd.reggie.dto.DishDto;
import com.xcd.reggie.entity.Category;
import com.xcd.reggie.entity.Dish;
import com.xcd.reggie.entity.DishFlavor;
import com.xcd.reggie.service.CategoryService;
import com.xcd.reggie.service.DishFlavorService;
import com.xcd.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info("获取到的信息：{}", dishDto);
        dishService.saveWithFlavor(dishDto);

        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Dish> dishPage = new Page<>(page, pageSize);

        Page<DishDto> dishDtoPage = new Page<>();

        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage, lambdaQueryWrapper);
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

        List<DishDto> dishDtos = dishPage.getRecords().stream().map(new Function<Dish, DishDto>() {
            @Override
            public DishDto apply(Dish dish) {
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(dish, dishDto);
                String name = categoryService.getById(dish.getCategoryId()).getName();
                dishDto.setCategoryName(name);
                return dishDto;
            }
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(dishDtos);
        return R.success(dishDtoPage);
    }

    @DeleteMapping
    public R<String> delete(Long[] ids) {
        log.info("ids:  {}", Arrays.toString(ids));
        dishService.removeArray(ids);
        return R.success("删除成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable("id") Long id) {
        log.info("id:  {}", id);
        DishDto dishDto = new DishDto();
        Dish dish = dishService.getById(id);
        Long categoryId = dish.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String categoryName = category.getName();
        dishDto.setCategoryName(categoryName);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(id != null, DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);
        dishDto.setFlavors(list);
        BeanUtils.copyProperties(dish,dishDto);

        return R.success(dishDto);
    }

    @PutMapping
    public R<String> put(@RequestBody DishDto dishDto){

        dishService.updateWithFlavor(dishDto);

        return R.success("修改成功");
    }
    @PostMapping("/status/{status}")
    public  R<String> updateStatus(@PathVariable("status") Integer status,@RequestParam("ids") Long[] ids){
        log.info("修改状态信息如下status: {}   ids:",status,ids);
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setStatus(status);
            dish.setId(id);
            dishService.updateById(dish);
        }
        return R.success("修改成功");
    }
//    @GetMapping("list")
//    public R<List<Dish>> listDish(Dish dish){
//        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
//        lambdaQueryWrapper.eq(Dish::getStatus,1);
//        lambdaQueryWrapper.orderByDesc(Dish::getSort);
//        List<Dish> list = dishService.list(lambdaQueryWrapper);
//        return R.success(list);
//    }
    @GetMapping("list")
    public R<List<DishDto>> listDish(Dish dish){
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        lambdaQueryWrapper.orderByDesc(Dish::getSort);
        List<Dish> list = dishService.list(lambdaQueryWrapper);
        List<DishDto> dishDtos = list.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,item.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dishDto.setFlavors(dishFlavorList);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null ){
                dishDto.setCategoryName(category.getName());
            }
            return  dishDto;
        }).collect(Collectors.toList());
         return R.success(dishDtos);
    }
}
